﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace PlaystationNetworkAPI.Core.Common
{

    /// <summary>
    /// Class that will handle the generic parsing features.
    /// </summary>
    class Parser
    {
        internal string _HtmlCode;
        internal string _RegexPattern;

        /// <summary>
        /// Parser constructor.
        /// </summary>
        /// <param name="htmlCode">Html code that will be parsed.</param>
        internal Parser(string htmlCode)
        {
            _HtmlCode = htmlCode;
            _RegexPattern = Properties.Settings.Default.Common_Parser_RegexPattern;
        }

        /// <summary>
        /// Get the inner text from a html tag.
        /// </summary>
        /// <param name="tagName">Name of the Html tag.</param>
        /// <param name="attributeName">Name of the Html tag attribute.</param>
        /// <param name="attributeValue">Value of the Html tag attribute.</param>
        /// <returns>List of maching results.</returns>
        internal List<string> GetInnerText(string tagName, string attributeName, string attributeValue)
        {
            //string regexPattern = @"<\s*" + tagName + @"\s*" + attributeName + @"\s*=\s*["",']" + attributeValue + @"["",']\s*[^>]\s*(?<innerText>\w+)\s*<\s*" + tagName + @"\s*[^>]";
            string regexPattern = string.Format(_RegexPattern, tagName, attributeName, attributeValue);

            MatchCollection matchCollection = Regex.Matches(_HtmlCode, regexPattern, RegexOptions.IgnoreCase);

            List<string> results = new List<string>();

            foreach (Match match in matchCollection)
            {
                results.Add(match.Groups["innerText"].Value);
            }

            return results;
        }

        /// <summary>
        /// Remove the line breaks from a string
        /// </summary>
        /// <param name="inputString">String that will have the line breaks removed</param>
        /// <returns>String with no line breaks</returns>
        internal string RemoveLineBreaks(string inputString)
        {
            return inputString.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty);
        }

    }
}
